<meta charset="UTF-8">
<?php
class User_controller extends CI_Controller{
    public function __construct(){
        parent::__construct(); 
        $this->load->model('user_model');
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        //var_dump($this->session->userdata('validated')) ;
        //$this->check_isvalidated();
    }
    
    public function index()
    {
        //var_dump($this->session->userdata);
        if($this->session->userdata['user_data'] == "user")
        {
            $data['EMAIL'] = $this->session->userdata['EMAIL'];
            $data['USERNAME'] = $this->session->userdata['USERNAME'];
            $data['FULLNAME'] = $this->session->userdata['FULLNAME'];
            $this->load->view("user_view/current_user/view_user", $data);
        }
        else {
            echo "Bạn phải đăng nhập trước đã";
        }
    }
   
    public function signup()
    {
        if(isset($_POST['email']) && $_POST['email']!="")
        {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $sha1pass = sha1($password);
            $fullname = $_POST['fullname'];
            $email = $_POST['email']; 
            $gender = $_POST['gender'];
            $year = $_POST['year_of_birth'];
            $school_level = $_POST['school_level'];
            $user_school = $_POST['user_school'];
            $general_course = $this->user_model->get_data_general_course();

            //echo $gender;
            if($this->user_model->check($email) == TRUE || $this->user_model->check($username) == TRUE)
                echo "email hoặc username đã được sử dụng ";
                
            else   
            {
                date_default_timezone_set('Asia/Ho_Chi_Minh');
                $date = date('Y-m-d H:i:s');
                $data = array(
                    'NAME'          => $username,
                    'FULLNAME'      => $fullname,
                    'PASSWORD'      => $sha1pass,
                    'EMAIL'         => $email,
                    'GENDER'        => $gender,
                    'YEAR_OF_BIRTH' => $year,
                    'CREATE_DATE'   => $date,
                    'SCHOOL_LEVEL' => $school_level,
                    'SCHOOL' => $user_school,
                    'UNREGISTED_COURSE_QUAN' => $general_course['course_quan'],
                    'UNREGISTED_ID_OF_COURSES' => $general_course['id_of_courses']
                );
                $this->user_model->insert_new_user($data);
                
                $user = $this->user_model->view_user($email);
                
                 
                $ids = $user['user']['reg_id_of_courses'];
                //echo $ids[2];
                $d1 = (int)$ids[0];
                $d2 = (int)$ids[1];
                $d3 = (int)$ids[2];
                $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                //echo $int_id_course;
                $j = 3;
                $i = 0;
                while ($int_convert != 0) {                    
                    $id_courses[$i] = $int_convert;
                    //echo $id_courses[$i]."</br>";
                    $d1 = (int)$ids[$j];
                    $j++;
                    $d2 = (int)$ids[$j];
                    $j++;
                    $d3 = (int)$ids[$j];
                    $j++;
                    $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                    $i++;
                }               
            
                $ids = $user['user']['unreg_id_of_courses'];
                //echo $ids[2];
                $d1 = (int)$ids[0];
                $d2 = (int)$ids[1];
                $d3 = (int)$ids[2];
                $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                //echo $int_id_course;
                $j = 3;
                $i = 0;
                while ($int_convert != 0) {
                    
                    $unreg_id_courses[$i] = $int_convert;
                    //echo $id_courses[$i]."</br>";
                    $d1 = (int)$ids[$j];
                    $j++;
                    $d2 = (int)$ids[$j];
                    $j++;
                    $d3 = (int)$ids[$j];
                    $j++;
                    $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                    $i++;
                }
                
                if ($user['user']['reg_course_quan'] == 0) {
                    $id_courses[0] = 0;
                }
                
                if ($user['user']['unreg_course_quan'] == 0) {
                    $unreg_id_courses[0] = 0;
                }

                
                $user['courses'] = $this->user_model->get_courses_by_id($id_courses, $unreg_id_courses);
                
                if ($user['user']['reg_course_quan'] == 0) {
                    $user['courses']['reg'] = null;
                }
                
                if ($user['user']['unreg_course_quan'] == 0) {
                    $user['courses']['unreg'] = null;
                }
                
                
                // Save into the cache for 500 minutes
                $this->load->driver('cache');
                $this->cache->apc->save('reg_id_of_courses', $user['user']['ID'], $user['user']['reg_id_of_courses'],3000);
                $this->cache->apc->save('unreg_id_of_courses', $user['user']['ID'], $user['user']['unreg_id_of_courses'],3000);                
                $this->cache->apc->save('user_reg_courses', $user['user']['ID'], $user['courses']['reg'], 30000);                
                $this->cache->apc->save('user_unreg_courses', $user['user']['ID'], $user['courses']['unreg'], 30000);              
                $this->cache->apc->save('user_reg_course_quan', $user['user']['ID'], $user['user']['reg_course_quan'], 30000);               
                $this->cache->apc->save('user_unreg_course_quan', $user['user']['ID'], $user['user']['unreg_course_quan'], 30000); 
                $this->cache->apc->save('user_course_name', $user['user']['ID'], $user['courses']['course_name'], 30000); 
                              
                $this->load->view("user_view/current_user/view_user", $user);
                     
            }   
        }
         else {
             redirect('');
         }
    }
    
    function login()
    {
        if(isset($_POST['email']) && $_POST['email']!="")
        {
            //$email = $_POST['email']; 
            //$password = $_POST['password'];
            $data['email'] = $_POST['email'];
            
            
            if ($_POST['type_of_user'] == "normal_user"){
            if($this->user_model->checklogin($_POST['email'], $_POST['password']) == TRUE)
            {
                $user = $this->user_model->view_user($_POST['email']);                
                 
                $ids = $user['user']['reg_id_of_courses'];
                //echo $ids[2];
                $d1 = (int)$ids[0];
                $d2 = (int)$ids[1];
                $d3 = (int)$ids[2];
                $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                //echo $int_id_course;
                $j = 3;
                $i = 0;
                while ($int_convert != 0) {                    
                    $id_courses[$i] = $int_convert;
                    //echo $id_courses[$i]."</br>";
                    $d1 = (int)$ids[$j];
                    $j++;
                    $d2 = (int)$ids[$j];
                    $j++;
                    $d3 = (int)$ids[$j];
                    $j++;
                    $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                    $i++;
                }              
            
                
                $ids = $user['user']['unreg_id_of_courses'];
                //echo $ids[2];
                $d1 = (int)$ids[0];
                $d2 = (int)$ids[1];
                $d3 = (int)$ids[2];
                $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                //echo $int_id_course;
                $j = 3;
                $i = 0;
                while ($int_convert != 0) {
                    
                    $unreg_id_courses[$i] = $int_convert;
                    //echo $id_courses[$i]."</br>";
                    $d1 = (int)$ids[$j];
                    $j++;
                    $d2 = (int)$ids[$j];
                    $j++;
                    $d3 = (int)$ids[$j];
                    $j++;
                    $int_convert = $d1 * 100 + $d2 * 10 + $d3;
                    $i++;
                }
                
                if ($user['user']['reg_course_quan'] == 0) {
                    $id_courses[0] = 0;
                }
                
                if ($user['user']['unreg_course_quan'] == 0) {
                    $unreg_id_courses[0] = 0;
                }

                
                $user['courses'] = $this->user_model->get_courses_by_id($id_courses, $unreg_id_courses);
                
                if ($user['user']['reg_course_quan'] == 0) {
                    $user['courses']['reg'] = null;
                }
                
                if ($user['user']['unreg_course_quan'] == 0) {
                    $user['courses']['unreg'] = null;
                }
                
                
                // Save into the cache for 500 minutes
                $this->load->driver('cache');
                $this->cache->apc->save('reg_id_of_courses', $user['user']['ID'], $user['user']['reg_id_of_courses'],3000);
                $this->cache->apc->save('unreg_id_of_courses', $user['user']['ID'], $user['user']['unreg_id_of_courses'],3000);                
                $this->cache->apc->save('user_reg_courses', $user['user']['ID'],$user['courses']['reg'], 30000);                
                $this->cache->apc->save('user_unreg_courses', $user['user']['ID'], $user['courses']['unreg'], 30000);              
                $this->cache->apc->save('user_reg_course_quan', $user['user']['ID'], $user['user']['reg_course_quan'], 30000);               
                $this->cache->apc->save('user_unreg_course_quan', $user['user']['ID'], $user['user']['unreg_course_quan'], 30000);
                $this->cache->apc->save('user_course_name', $user['user']['ID'], $user['courses']['course_name'], 30000);
                  
                              
                $this->load->view("user_view/current_user/view_user", $user);
            }
            else 
            {
                echo "<script>";
                echo "alert('Tên đăng nhập hoặc mật khẩu hoặc loại tài khoản không chính xác');";
                echo "</script>";
                //redirect('');
            }
            } else {
                if ($_POST['type_of_user'] == "in_charge_teacher") {
                    $user = $this->user_model->checklogin_incharge_teacher($_POST['email'], $_POST['password']);
                    if($user['user']['validated'] == TRUE) {
                        $this->load->view("user_view/current_user/view_in_charge_teacher", $user);
                    } else {
                        echo "<script>";
                        echo "alert('Tên đăng nhập hoặc mật khẩu hoặc loại tài khoản không chính xác.');";
                        echo "</script>";
                        //redirect('home_controller/index');
                    }
                }
            }
        }
        else {
            redirect('');
        }
    }
    
    function signup_course($id_signup_course){
        $this->load->library('session');
        $user['user'] = $this->session->all_userdata();
        if ($user['user']['EMAIL'] != null){
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            
            $reg_course_quan = $this->cache->apc->get('user_reg_course_quan', $user['user']['ID']);            
            $courses = $this->cache->apc->get('user_reg_courses', $user['user']['ID']);
            //cap nhat lai gia tri cua chuoi id_of_courses
            $j = 0;
            while (($j < $reg_course_quan) && ($id_signup_course > $courses[$j]['course_id'])) {
                //echo $courses[$j]['course_id'];
                $j++;
            }
            
            //Them khoa hoc moi vao doan ma khoa hoc dang ky
            if ((!isset($courses[$j]['course_id'])) || ($id_signup_course != $courses[$j]['course_id'])) {
                
                $unreg_course_quan = $this->cache->apc->get('user_unreg_course_quan', $user['user']['ID']); 
                $reg_id_of_courses = $this->cache->apc->get('reg_id_of_courses', $user['user']['ID']);
                $unreg_id_of_courses = $this->cache->apc->get('unreg_id_of_courses', $user['user']['ID']);
            
                $i = $j * 3;
                $courses[$reg_course_quan] = null;
                $temp_course = $courses[$j]; 
                $courses[$j] = $this->user_model->get_single_course_by_id($id_signup_course);
                $course_signup_code = $courses[$j]['course_code'];           
                $d1 = $reg_id_of_courses[$i];
                $d2 = $reg_id_of_courses[$i+1];
                $d3 = $reg_id_of_courses[$i+2];
                $str_id_signup_course = (string) $id_signup_course; 
                if ($id_signup_course < 10) {
                    $reg_id_of_courses[$i] = 0;
                    $reg_id_of_courses[$i+1] = 0;
                    $reg_id_of_courses[$i+2] = $str_id_signup_course[0];
                } else {
                    if ($id_signup_course < 100) {
                        $reg_id_of_courses[$i] = 0;                    
                        $reg_id_of_courses[$i+1] = $str_id_signup_course[0];
                        $reg_id_of_courses[$i+2] = $str_id_signup_course[1];
                    } else {
                        $reg_id_of_courses[$i] = $str_id_signup_course[0];
                        $reg_id_of_courses[$i+1] = $str_id_signup_course[1];
                        $reg_id_of_courses[$i+2] = $str_id_signup_course[2];
                    }
                }
            
                $i = $i + 3;
                $j++;
            
                while ($j <= $reg_course_quan) {
                    $temp = $d1;
                    $d1 = $reg_id_of_courses[$i]; 
                    $reg_id_of_courses[$i] = $temp;
                
                    $temp = $d2;
                    $d2 = $reg_id_of_courses[$i + 1];
                    $reg_id_of_courses[$i+1] = $temp;
                
                    $temp = $d3;
                    $d3 = $reg_id_of_courses[$i + 2];
                    $reg_id_of_courses[$i + 2] = $temp;
                
                    $temp = $temp_course;
                    $temp_course = $courses[$j];
                    $courses[$j] = $temp;
                
                    $j++;
                    $i = $i + 3;
                }
            
                $reg_course_quan++;
            
                $this->cache->apc->save('reg_id_of_courses', $user['user']['ID'], $reg_id_of_courses, 3000);
                $this->cache->apc->save('user_reg_course_quan', $user['user']['ID'], $reg_course_quan, 30000);
            
                if ($reg_course_quan == 0) {
                    $courses = null;
                }
            
                $user['courses']['reg'] = $courses;

                $this->cache->apc->save('user_reg_courses', $user['user']['ID'], $courses, 3000);
                
                //update the unreg courses
                $courses = $this->cache->apc->get('user_unreg_courses', $user['user']['ID']);
                $courses[$unreg_course_quan] = null;
                //Cap nhat lai gia tri cua chuoi unreg_id_of_courses
                $j = 0;
                while (($j < $unreg_course_quan) && ($id_signup_course > $courses[$j]['course_id'])) {
                    $j++;
                }
                $i = $j * 3;
                while ($j < $unreg_course_quan) {
                    $unreg_id_of_courses[$i] = $unreg_id_of_courses[$i + 3];
                    $unreg_id_of_courses[$i + 1] = $unreg_id_of_courses[$i + 4];
                    $unreg_id_of_courses[$i + 2] = $unreg_id_of_courses[$i + 5];
                    $courses[$j] =  $courses[$j+1];
                    $i = $i + 3;
                    $j++;                
                }

                $unreg_course_quan--;
                $this->cache->apc->save('user_unreg_course_quan', $user['user']['ID'], $unreg_course_quan, 30000); 
                $this->cache->apc->save('unreg_id_of_courses', $user['user']['ID'], $unreg_id_of_courses, 3000);
                $this->cache->apc->save('user_unreg_courses', $user['user']['ID'], $courses, 30000);
                 
                //Luu du lieu moi ve khoa hoc vao co so du lieu 
                $user['courses']['unreg'] = $courses;
                $this->user_model->update_course($user['user']['ID'], $user['user']['EMAIL'], $reg_id_of_courses, $unreg_id_of_courses, $reg_course_quan, $unreg_course_quan, $course_signup_code);
            
                $user['user']['unreg_course_quan'] = $unreg_course_quan; 
                $user['user']['reg_id_of_courses'] = $reg_id_of_courses;
                $user['user']['unreg_id_of_courses'] = $unreg_id_of_courses;
            
            } else {
                $user['courses']['reg'] = $this->cache->apc->get('user_reg_courses', $user['user']['ID']);
                $user['courses']['unreg'] = $this->cache->apc->get('user_unreg_courses', $user['user']['ID']);
                $user['user']['unreg_course_quan'] = $this->cache->apc->get('user_unreg_course_quan', $user['user']['ID']); 
                $user['user']['reg_id_of_courses'] = $this->cache->apc->get('reg_id_of_courses', $user['user']['ID']);
                $user['user']['unreg_id_of_courses'] = $this->cache->apc->get('unreg_id_of_courses', $user['user']['ID']);            
            }
            
            $user['courses']['course_name'] = $this->cache->apc->get('user_course_name', $user['user']['ID']);
            
            $user['user']['reg_course_quan'] = $reg_course_quan;      
      
            $this->load->view("user_view/current_user/view_user", $user);      
            
        } else {
            ?> <script>
                alert("Bạn chưa đăng nhập")
            </script> <?php
        }     
        
    }
    
    //CHUC NANG NAY CHUA HOAN THANH 
    function destroy_course($destroy_id){        
    }
    
            
    function view_user( ){
        $this->load->library('session');
        $user['user'] = $this->session->all_userdata();
        if ($user['user']['EMAIL'] != null){
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            
            $user['user']['reg_course_quan'] = $this->cache->apc->get('user_reg_course_quan', $user['user']['ID']);            
            $user['courses']['reg'] = $this->cache->apc->get('user_reg_courses', $user['user']['ID']);
            $user['courses']['unreg'] = $this->cache->apc->get('user_unreg_courses', $user['user']['ID']);
            $user['user']['unreg_course_quan'] = $this->cache->apc->get('user_unreg_course_quan', $user['user']['ID']); 
            $user['user']['reg_id_of_courses'] = $this->cache->apc->get('reg_id_of_courses', $user['user']['ID']);
            $user['user']['unreg_id_of_courses'] = $this->cache->apc->get('unreg_id_of_courses',$user['user']['ID']);  
            $user['courses']['course_name'] = $this->cache->apc->get('user_course_name', $user['user']['ID']);      
              
            $this->load->view("user_view/current_user/view_user", $user);
            
        } else {
            ?> <script>
                alert("Bạn chưa đăng nhập")
            </script> <?php
        }
                
    }
    
    function change_public_inf($type_of_user) {
        $user['user'] = $this->session->all_userdata();
        
        if ($user['user']['EMAIL'] != null){
        switch ($type_of_user) {
            case "normal_user":
                $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            
                $user['user']['reg_course_quan'] = $this->cache->apc->get('user_reg_course_quan', $user['user']['ID']);            
                $user['courses']['reg'] = $this->cache->apc->get('user_reg_courses', $user['user']['ID']);
                $user['courses']['unreg'] = $this->cache->apc->get('user_unreg_courses', $user['user']['ID']);
                $user['user']['unreg_course_quan'] = $this->cache->apc->get('user_unreg_course_quan', $user['user']['ID']); 
                $user['user']['reg_id_of_courses'] = $this->cache->apc->get('reg_id_of_courses', $user['user']['ID']);
                $user['user']['unreg_id_of_courses'] = $this->cache->apc->get('unreg_id_of_courses', $user['user']['ID']);  
                $user['courses']['course_name'] = $this->cache->apc->get('user_course_name', $user['user']['ID']);    

                $allow_inf = (string) $_POST['allow_fullname'].$_POST['allow_school_name'].$_POST['allow_year_birth'].$_POST['allow_email'];
                //echo $allow_inf;
        
                $this->user_model->update_allow_inf($user['user']['ID'], $allow_inf, $type_of_user);
                
                $this->load->view("user_view/current_user/view_user", $user);
                break;
            case "in_charge_teacher":
                $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
                $allow_inf = (string) $_POST['allow_fullname'].$_POST['allow_birthday'].$_POST['allow_email'].$_POST['allow_phone'];
                $this->user_model->update_allow_inf($user['user']['SQL_ORDER'], $allow_inf, $type_of_user);
                $this->load->view("user_view/current_user/view_in_charge_teacher", $user);
                break; 
            default:
                break;    
        }
        } else {
            
        }
    }
    
    
    function change_pass($type_of_user) {
        $user['user'] = $this->session->all_userdata();
        
        if ($user['user']['EMAIL'] != null){
            switch($type_of_user) {
                case "normal_user":
                    $user['old_pass'] = $_POST['old_pass'];
                    $user['new_pass'] = $_POST['new_pass'];
                    $user['new_pass_check'] = $_POST['new_pass_check'];
        
                    $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            
                    $user['user']['reg_course_quan'] = $this->cache->apc->get('user_reg_course_quan', $user['user']['ID']);            
                    $user['courses']['reg'] = $this->cache->apc->get('user_reg_courses', $user['user']['ID']);
                    $user['courses']['unreg'] = $this->cache->apc->get('user_unreg_courses', $user['user']['ID']);
                    $user['user']['unreg_course_quan'] = $this->cache->apc->get('user_unreg_course_quan', $user['user']['ID']); 
                    $user['user']['reg_id_of_courses'] = $this->cache->apc->get('reg_id_of_courses', $user['user']['ID']);
                    $user['user']['unreg_id_of_courses'] = $this->cache->apc->get('unreg_id_of_courses', $user['user']['ID']);  
                    $user['courses']['course_name'] = $this->cache->apc->get('user_course_name', $user['user']['ID']);     

                    if ($this->user_model->check_pass($user['user']['ID'], $user['old_pass']) == FALSE) {
                        ?> <script>
                        alert("Mật khẩu cũ nhập không đúng");</script> 
                        <?php 
                    } else {
                        $this->user_model->reset_pass($user['user']['ID'], $user['new_pass']);
                    }
                
                    $this->load->view("user_view/current_user/view_user", $user);
                    break;
            }
        } else {
            
        }
    }
            
    function reset_pass() {
        $this->form_validation->set_rules('oldpassword', 'Oldpass', 'required');
        $this->form_validation->set_rules('newpassword', 'Newpass', 'required');
        $this->form_validation->set_rules('repassword', 'Repass', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view("frontend/view_resetpass");
        }
        else 
        {
            $data['oldpassword'] = $_POST['oldpassword'];
            $data['newpassword'] = $_POST['newpassword'];
            $data['repassword'] = $_POST['repassword'];
            $this->user_model->resetpass($data);;
        }
            
            //$this->user_model->login($password);

    }    

    public function log_out()
    {
        /*
        *load helper 
        */
        $this->load->library('session');
        $user_id = $this->session->userdata['ID'];
        $this->session->sess_destroy();
        
        $this->load->helper('html');
        $this->load->helper('url');
        /*
         * load database
         */
        $this->load->database();
        //Tai du lieu ve cac truong tham gia
        $this->load->model('Jskull_model');
        $add_data['jskull'] = $this->Jskull_model->get_all_skulls();
        //Tai du lieu ve cac khoa hoc hien co
        $this->load->model('Course_model');
        $add_data['course'] = $this->Course_model->get_all_courses();
        
        //Xoa cac file cache
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $this->cache->apc->delete('reg_id_of_courses', $user_id);
        $this->cache->apc->delete('unreg_id_of_courses', $user_id);
        $this->cache->apc->delete('user_reg_courses', $user_id);
        $this->cache->apc->delete('user_unreg_courses', $user_id);
        $this->cache->apc->delete('user_reg_course_quan', $user_id);
        $this->cache->apc->delete('user_unreg_course_quan', $user_id);
        $this->cache->apc->delete('user_course_name', $user_id);
        $this->cache->apc->delete('subject_of_new_ques', $user_id);
        $this->cache->apc->delete('title_of_new_ques', $user_id);
        $this->cache->apc->delete('txt_of_new_ques', $user_id);
        $this->cache->apc->delete('disc_ques', $user_id);
        $this->cache->apc->delete('txt_new_ans', $user_id);
        $this->cache->apc->delete('current_course', $user_id);
        $this->cache->apc->delete('current_lectures', $user_id);
        $this->cache->apc->delete('current_course_id', $user_id);
        $this->cache->apc->delete('current_course_name', $user_id);       

        
        $this->load->view('home_view/home_view', $add_data);
    }
    
    function change_school_name() {
        $user['user'] = $this->session->all_userdata();
        
        if ($user['user']['EMAIL'] != null){
            
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            
            $user['user']['reg_course_quan'] = $this->cache->apc->get('user_reg_course_quan', $user['user']['ID']);            
            $user['courses']['reg'] = $this->cache->apc->get('user_reg_courses', $user['user']['ID']);
            $user['courses']['unreg'] = $this->cache->apc->get('user_unreg_courses', $user['user']['ID']);
            $user['user']['unreg_course_quan'] = $this->cache->apc->get('user_unreg_course_quan', $user['user']['ID']); 
            $user['user']['reg_id_of_courses'] = $this->cache->apc->get('reg_id_of_courses', $user['user']['ID']);
            $user['user']['unreg_id_of_courses'] = $this->cache->apc->get('unreg_id_of_courses', $user['user']['ID']);  
            $user['courses']['course_name'] = $this->cache->apc->get('user_course_name', $user['user']['ID']); 
            $user['user']['school'] = $_POST['new_school_name'];
            //$this->load->model('User_model');
            $this->user_model->change_school_name($user['user']['ID'], $user['user']['school']);
            $this->load->view("user_view/current_user/view_user", $user);  
        } else {
            
        }
    }

    private function check_isvalidated(){
        if(! $this->session->userdata('validated')){
            redirect('');
        }
    }
    
    function update_course_for_who_cant_take_third_course() {
        $this->load->model('User_model');
        $this->User_model->update_course_for_who_cant_take_third_course();
    }
    
    function make_pdf_file_for_user_table() {
        $this->load->model('User_model');
        $add_data['user'] = $this->User_model->make_pdf_file_for_user_table();
        $this->load->view('user_view/create_data_user_table_pdf', $add_data);
    }
    
    function update_email_grade() {
        $this->load->model('user_model');
        $this->user_model->update_email_grade();
    }
    
    function view_user_grade() {
        $add_data['user'] = $this->session->all_userdata();
        if ($add_data['user']['EMAIL'] != null){
            $add_data['courses']['reg'] = $this->cache->apc->get('user_reg_courses', $add_data['user']['ID']);
            //var_dump($add_data['courses']['reg']);
            $j = 0;
            while(isset($add_data['courses']['reg'][$j]['course_code'])) {
                //echo $user['courses']['reg'][$j]['course_code'];
                $table_grade_name = $this->convert_grade_table_name($add_data['courses']['reg'][$j]['course_code']);
                //echo $abc;
                $this->load->model('update_grade_model');
                $add_data['grade_pupil'][$j] = $this->update_grade_model->get_grade_student
                    ($this->session->userdata['EMAIL'], $table_grade_name);
                $j++;    
            }
            $add_data['user']['reg_course_quan'] = $this->cache->apc->get('user_reg_course_quan', $add_data['user']['ID']); 
            $this->load->view("user_view/current_user/view_user_grade", $add_data);
            
        } else {
            
        }
    }
    
}
?>
